package com.steven.test.web.controller;

import com.qin.annotation.CallLog;
import com.steven.test.enums.ResponseEnum;
import com.steven.test.exception.GlobalException;
import com.steven.test.exception.GlobalViewException;
import com.steven.test.web.request.BasicRequest;
import com.steven.test.web.response.ResponseData;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 常见controller请求及处理方式
 *
 * @author qinxh
 * @date 2019/05/31
 */
@RestController
@RequestMapping("/basic")
public class BasicController {

    /**
     * 基本get请求
     *
     * @return String
     */
    @GetMapping("")
    @CallLog
    public String basicUrl(){

        return "this is index";
    }

    @GetMapping("/notResult")
    @CallLog
    public void notResult() {
        System.out.println("dsdsdsdsdsdsd");
    }

    /**
     * 统一返回结果集及枚举和获取请求路径中参数值
     *
     * @return ResponseData
     */
    @GetMapping("/{enumType}")
    public ResponseData basicResponseData(@PathVariable("enumType") String enumType){

        String success = "success",error = "error",status = "status";

        if(success.equals(enumType)){
            return ResponseData.ofSuccess("success");
        }
        if(error.equals(enumType)){
            return ResponseData.ofMessage(600, "自定义返回状态码和信息");
        }
        if(status.equals(enumType)){
            return ResponseData.ofStatus(ResponseEnum.MULTIPART_ERROR);
        }

        return null;
    }

    /**
     * 基本get带参数请求方式
     *
     * @return ResponseData
     */
    @GetMapping("/param")
    public ResponseData basicGetRequest(@RequestParam("param") String param){

        return ResponseData.ofSuccess(param);
    }

    /**
     * 基本post请求方式，通过实体接受参数（前端请求需要在header部分加上 Content-Type:application/json）
     *
     * @return ResponseData
     */
    @PostMapping("")
    public ResponseData basicPostRequest(@RequestBody BasicRequest basicRequest){

        if(basicRequest!=null){
            return ResponseData.ofSuccess(basicRequest);
        }

        throw new GlobalException(ResponseEnum.PARAM_EMPTY);
    }

    /**
     * 自定义全局异常测试
     *
     * @return ResponseData
     */
    @GetMapping("/globalException")
    public ResponseData basicGlobalException(){

        throw new GlobalException(ResponseEnum.SERVER_ERROR);
    }

    /**
     * 自定义全局视图异常测试
     * @return
     */
    @GetMapping("/globalViewException")
    public ResponseData basicGlobalViewException() {

        throw new GlobalViewException("自定义全局视图异常测试");
    }
}